Hello, data example below: I have one row per ID, the baseline date (BSDATE), and the date until which they are followed (FUuntil)
I wrote the following code to create some wide variables for each person (if a person is followed from 4jan1990 to 5sept1992, i would want them to have 3 "from" and "to" date pair variables as follows:
dfrom1= 4 jan 1990; dto1=31Dec1990
dfrom2= 1jan1991; dto2=31dec1991
dfrom3=1jan1992; dto3=5sept1992
but it wasnt working how i wanted it to. [PS: i looped 36 times as the largest # years of follow up is 36]
But when i changed it to this it worked.
Not sure what i was doing wrong the first time? the structured code like that really helps me think more clearly, so i was bummed that i had to convert it to the other type of conditional in order for it to work..would appreciate any help!!
Code:
* Example generated by -dataex-. For more info, type help dataex clear input float id long BSDATE float FUuntil 1 8886 12768 2 8873 9742 3 8868 12744 4 8866 21662 5 8883 11760 6 8862 20045 7 8862 20315 8 8889 11933 9 8861 9713 10 8872 12715 end format %td BSDATE format %td FUuntil
dfrom1= 4 jan 1990; dto1=31Dec1990
dfrom2= 1jan1991; dto2=31dec1991
dfrom3=1jan1992; dto3=5sept1992
but it wasnt working how i wanted it to. [PS: i looped 36 times as the largest # years of follow up is 36]
Code:
sum FUtime //36
gen curryear=year(BSDATE)
foreach x of numlist 1/36 {
gen dfrom`x'=.
if curryear==year(BSDATE){
replace dfrom`x'=BSDATE
}
else if curryear>year(BSDATE) & !mi(curryear) & curryear<=year(FUuntil) {
replace dfrom`x'=mdy(1,1,curryear)
}
gen dto`x'=.
if curryear<year(FUuntil){
replace dto`x' =mdy(12,31,curryear)
}
else if curryear==year(FUuntil){
replace dto`x'=FUuntil
}
format dfrom`x' %td
format dto`x' %td
dis curryear
replace curryear=curryear+1
}
But when i changed it to this it worked.
Code:
gen curryear=year(BSDATE)
foreach x of numlist 1/36 {
gen dfrom`x'=.
replace dfrom`x'=BSDATE if curryear==year(BSDATE)
replace dfrom`x'=mdy(1,1,curryear) if curryear>year(BSDATE) & !mi(curryear) & curryear<=year(FUuntil)
gen dto`x'=.
replace dto`x' =mdy(12,31,curryear) if curryear<year(FUuntil)
replace dto`x'=FUuntil if curryear==year(FUuntil)
format dfrom`x' %td
format dto`x' %td
dis curryear
replace curryear=curryear+1
}

Comment